Code Julia sets
//9.1
//Julia set of z*z+c, Monte Carlo procedureint scaleFactor=150; //corresponds to window (-7.5,-5)-(7.5,5)
int
offsetX=250, offsetY=150;
int k=0;
float a=-1,b=0,x=random(1),y=random(1); //Julia set parameters
void setup(){
size(500, 300);
background(255,255,255,0);
}
void draw(){
for (int k = 0; k < 100; k = k+1) {
float x1=(x-a)/2,y1=(y-
b)/2;
float r=sqrt(x1*x1+y1*y1);
float q=random(1);
if(q<.5){
x=sqrt(r+x1);y=sqrt(r-x1);
if(y1<0){y=-y;}
}
else
{
x=-sqrt(r+x1);y=-sqrt(r-x1);
if(y1<0){y=-y;}
}
int X=int(scaleFactor*x+offsetX);
int Y=int(scaleFactor*y
+offsetY);
if(k>10){point(X,Y);point(-X,-Y);}
if (k>10 && b==0){point(X,-Y);point(-X,Y);}
}}9.3
float A=-.8,B=.15; //parameters
float DELH=1.6,DELV=1.0,k=0;
int KMAX=200;
float N1=250.0;boolean donotPrint =
false;void setup(){
size(550,350);
background(255);
stroke(0);
noLoop();
}
void draw(){
translate
(width/2,height/2);
int N2=int(N1*DELV/DELH),N3;
if (B==0){N3=0;}
else {N3 = -N2;}
for(int I=0;I<N1+1;I++){
for
(int J=N3;J<N2+1;J++){
float X=I*DELH/N1,Y=J*DELV/N2;
for(int K=1;K<KMAX;K++){
float X1=X*X-Y*Y+A;
float Y1=2*X*Y+B;
float S=X*X+Y*Y;
if(S>1000){
donotPrint=true;
break;} //exit loop
else{
X=X1;
Y=Y1;
}
}
if (donotPrint){donotPrint=false;} //do not print a point and reset variable
else {
point(I,J);point(-I,-J);
if (B==0){
point(I,-J);point(-I,J);
}
}
}
}
}
void
mousePressed(){
k=k+1;
int rem=int(k%9);
switch(rem){
case 0:
A=-
1.275;B=0;DELH=1.8;DELV=.8;N1=240;
break;
case 1:
A=-1;B=0;DELH=1.7;DELV=1;N1=240;
break;
case 2:
A=-.75;B=0;DELH=1.6;DELV=1.1;N1=200;
break;
case 3:
A=.25;B=0;DELH=1;DELV=1.3;N1=120;
break;
case 4:
A=-.3905;B=.5868;DELH=1.5;DELV=1.2;N1=200;
break;
case 5:
A=-.1226;B=.7449;DELH=1.4;DELV=1.2;N1=160;
break;
case 6:
A=-.11;B=.67;DELH=1.4;DELV=1.3;N1=160;
break;
case 7:
A=.3;B=.04;DELH=1.3;DELV=1.3;N1=160;
break;
default:
A=-.8;B=.15;DELH=1.6;DELV=1;N1=250 ;
break;
}
background(255);
redraw();
}/*from H.A. Lauwerier Graphics&Fractals, 1994 program JULIAPX translated into
Processing by J.G.van Unnik,
2010*/
//Julia fractals
float A=-1.029,B=.3863,DELH=2,DELV=1.2,x,x1,y,y1,s,s1;
int
k,n,KMAX=100,L=0,N1=240,N2=int(N1*DELV/DELH),N3,p=1;
int[] COL =
{#000000,#0000AA,#5555FF,#00AA00,#55FF55,#AA0000,#FF5555,#AA5500,#FFFF55,};//8 EGA colors
void
setup(){
size(400,300);
noLoop();
}
void draw(){
translate(width/2,height/2);
if(B==0){N3=0;}
else{N3=N2;}
for (int
i=0;i<=N1;i++){
for(int j=-N3;j<=N2;j++){
x=i*DELH/N1;y=j*DELV/N2;
for(k=1;k<=KMAX;k++){
x1=sq
(x)-sq(y)+A;
y1=2*x*y+B;
s=sq(x)+sq(y);
s1=sq(x-x1)+sq(y-y1);
if(s>1000){
L=1+int
(k/p)%8;
k=KMAX+1; //exit for
}
if(s1<.0001){
L=1+int(k/p)%8;
k=KMAX+1; //exit for
}
else{x=x1;y=y1;}
if(k==KMAX){L=0;}
}
//graphics
stroke(COL[L]);
point(i,j);
point(-i,-j);
if(B==0){
point(-i,j);
point(i,-j);
}
}
}
}
void mousePressed(){
n = n
+1;
int rem=int(n%6);
switch(rem) {
case 1:
A=-.55;B=0;DELH=1.6;DELV=1.2;KMAX=100;N1=200;p=1;
break;
case 2:
A=-.75;B=0;DELH=1.8;DELV=1.2;KMAX=50;N1=240;p=1;
break;
case 3:
A=-.7454;B=.1103;DELH=1.6;DELV=1;KMAX=200;N1=200;p=5;
break;
case 4:
A=-.1;B=.3;DELH=1.5;DELV=1.5;KMAX=100;N1=200;p=1;
break;
case 5:
A=-.1226;B=.7449;DELH=1.7;DELV=1.7;KMAX=100;N1=200;p=1;
break;
default:
A=-
1.029;B=.3863;DELH=2;DELV=1.2;KMAX=100;N1=240;p=1;
break;
}
background(0);
redraw();
}//from H.A. Lauwerier Graphics&Fractals, 1994 program Mandelx2 translated into
Processing by J.G.van Unnik,
float A=0.35,B=0,DELH=.95,DELV=.95,EPS=.000001,X,Y;
int L,N1=160,N2=int(N1*DELV/DELH),N3;
int[] COL =
{#000000,#0000AA, #5555FF, #AA0000, #FF5555, #00AA00, #55FF55};
boolean prnt=true;void setup(){
size
(320,320);
noLoop();
}
void draw(){
translate(width/2,height/2);
if(B==0){N3=0;}
else{N3=N2;}
for(int i=0;i<=N1;i+
+){
for (int j=-N3;j<=N2;j++){
X=i*DELH/N1;Y=j*DELV/N2;
for(int k=1;k<100;k++){
float S=sq(X)+sq
(Y);
if(S>40){prnt=false;
k=100;}
float x2=sq(X)-sq(Y),y2=2*X*Y;
float x4=sq(x2)-sq
(y2),y4=2*x2*y2;
float x8=sq(x4)-sq(y4),y8=2*x4*y4;
float u=x8+x4+A;
float v=y8+y4+B;
float
d1=abs(X-u), d2=abs(Y-v);
if(d1<EPS&&d2<40*EPS){L=k%6;
k=100;
prnt=true;}
if(d2<EPS&&d1<40*EPS){L=k%6;
k=100;
prnt=true;}
X=u;Y=v;
}
if(prnt){
stroke(COL[L]);
point(i,-j);point(-i,j);
if(B==0){
point(i,j);point(-i,-j);}
prnt=false;
}
}
}
}